package com.wm.web.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Scriptorium;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.ScriptoriumService;
import com.wm.service.UserService;
import com.wm.web.servlet.ShopkeeperSession;
import com.wm.web.servlet.UserCookie;
import com.wm.web.servlet.UserSession;

public class ShopTopAction extends BaseAction implements SessionAware{

	private static final Log logger = LogFactory.getLog(ShopTopAction.class);

	private UserService userService;
	private ScriptoriumService scriptoriumService;

	public ScriptoriumService getScriptoriumService() {
		return scriptoriumService;
	}

	public void setScriptoriumService(ScriptoriumService scriptoriumService) {
		this.scriptoriumService = scriptoriumService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	private String username;
	private String password;
	private String returnURL;
	private String name;
	private long id;
	private String resName;
	private int userType; //用户注册状态，完成注册是0，只注册了用户名密码，没完善信息是1

	public int getUserType() {
		return userType;
	}

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public String getResName() {
		return resName;
	}

	public void setResName(String resName) {
		this.resName = resName;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getReturnURL() {
		return returnURL;
	}

	public void setReturnURL(String returnURL) {
		this.returnURL = returnURL;
	}
	
	private Map session;
	
    public void setSession(Map session){
    	this.session = session;
    }
    
    public Map getSession() {
		return session;
	}
    
	public String execute() {
		try {
//			if(this.getSession().get(Constant.SHOPKEEPER_SESSION_KEY)==null && Constant.USER_COOKIES){
//				HttpServletRequest request = ServletActionContext.getRequest();
//				HttpServletResponse response = ServletActionContext.getResponse();
//				UserCookie userCookie = new UserCookie(request,response);
//				String uid = userCookie.getCookie(Constant.SHOPKEEPER_SESSION_KEY);
//				if(uid!=null && !uid.trim().equals("")){
//					User u = this.getUserService().findUserById(new Long(uid));
//					if(u!=null){
//						UserSession us = new UserSession();
//						us.setUid(u.getId());
//						us.setEmail(u.getUserEmail());
//						us.setUsername(u.getUserName());
//						this.session.put(Constant.SHOPKEEPER_SESSION_KEY, us);
//						this.setUsername(u.getUserName());
//					}
//				}
//			}
			
//			//查cookie记录
//			HttpServletRequest request = ServletActionContext.getRequest();
//			HttpServletResponse response = ServletActionContext.getResponse();
//			UserCookie userCookie = new UserCookie(request,response);
//			String scrId = userCookie.getCookie(Constant.SCR_COOKIE_KEY);
//			if(scrId!=null && !scrId.equals("")){
//				this.setId(Integer.valueOf(scrId));
//				//查楼宇
//				Scriptorium scriptorium = this.getScriptoriumService().findScriptoriumById(this.getId());
//				if(scriptorium!=null){
//					this.setName(scriptorium.getScrName());
//				}
//			}
			ShopkeeperSession ss = (ShopkeeperSession)this.getSession().get(Constant.SHOPKEEPER_SESSION_KEY);
			return "index";
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

}
